package com.glory.wxelectricitypda.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LotCheckListAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.PackBean;
import com.glory.wxelectricitypda.bean.ResponseQueryBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class LotCheckActivity extends BaseDialogActivity {


    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private LotCheckListAdapter lotCheckListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_check;
    }

    @Override
    protected void initView() {
        rvLotList.setLayoutManager(new LinearLayoutManager(LotCheckActivity.this));
        rvLotList.setItemAnimator(new DefaultItemAnimator());
        rvLotList.addItemDecoration(new DividerItemDecoration(LotCheckActivity.this, 1));
        lotCheckListAdapter = new LotCheckListAdapter(null);
        lotCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lotCheckListAdapter.isFirstOnly(true);
        rvLotList.setAdapter(lotCheckListAdapter);

        freeUI_titleView.setTitle("月末盘点");
    }


    @Override
    protected void initData() {

        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    boolean isHave = false;
                    for (PackBean packBean : lotCheckListAdapter.getData()) {
                        if (packBean.getLOTID().equalsIgnoreCase(s.toString())) {
                            isHave = true;
                            break;
                        }
                    }
                    if (isHave) {
                        showToast("批次号已存在列表中", false);
                    } else {
                        requestLotCheck(s.toString());
//                        String userName = etUser.getText().toString();
//                        if (userName.isEmpty()){
//                            showToast("请输入操作人工号",false);
//                            return;
//                        }
//                        requestUser(userName, new CallBack() {
//                            @Override
//                            public void success() {
//                                requestLotCheck(s.toString());
//                            }
//
//                            @Override
//                            public void fail() {
//                                showToast("操作人工号验证失败，请重新输入", false);
//                            }
//
//                            @Override
//                            public void error() {
//                                showToast("连接不到服务器，请重试", false);
//                            }
//                        });
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestLotCheck(final String lotId) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotId);
            jsonObject1.put("ACTIONTYPE", "PANDIAN");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<ResponseQueryBean>>(
                new TypeToken<ResponseBean<ResponseQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ResponseQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    requestLotData(lotId);
                }
            }

            @Override
            public void onError(Response<ResponseBean<ResponseQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "月末盘点");
            }
        });
    }

    private void requestLotData(String lotId) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("LOTID", lotId);
            jsonObject1.put("ACTIONTYPE", "GETLOTINFO");
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITLOTQUERY");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<ResponseQueryBean>>(
                new TypeToken<ResponseBean<ResponseQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ResponseQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    lotCheckListAdapter.addData(0, response.body().getBody().getLOT());
                    etLotId.requestFocus();
                    etLotId.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<ResponseQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "GETLOTINFO");
            }
        });
    }

}
